package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.service.RoleResourcesService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 角色资源映射 控制器类
 * @author  y
 */
@Controller
@Action(ownermodel = SysAuditModelType.ROLE_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT+UrlConstants.PROJECT_MODULE_SYSTEM+"/roleResources")
public class RoleResourcesController extends SysBaseController {
	@Resource
	private RoleResourcesService roleResourcesService;

	/**
	 * 角色添加资源
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return ResultData
	 */
	@Action(description = "角色添加资源",
			detail = "给角色<#assign entity=sysRoleService.get(roleId)/>【${entity.roleName}】修改资源<#if success>成功<#else>失败</#if>",
			exectype = "编辑日志")
	@ResponseBody
	@PostMapping(UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
	public ResultData add(HttpServletRequest request, HttpServletResponse response){
		try{
			String roleId = RequestUtil.getString(request, "roleId");
			String[] resId = RequestUtil.getStringAryByStr(request, "resId");
			roleResourcesService.roleAddRes(roleId,resId);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("save.success");
		}catch (Exception e){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError(e.getMessage());
		}
	}

}
